package com.smarttest.portal.refresh.onlinequery.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import com.smarttest.portal.beans.InterfaceDetail;
import com.smarttest.portal.refresh.onlinequery.dao.OpenAPIDao;

@Repository
public class OpenAPIDaoImpl implements OpenAPIDao {
	private DataSource dataSource;

	@Resource(name = "dataSourceOpenAPI")
	protected void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public List<InterfaceDetail> getAllInterface(String searchValue) {
		String table = "openapi_interface";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			List<InterfaceDetail> resultList = new ArrayList<InterfaceDetail>();
			conn = dataSource.getConnection();
			String sql = "select id,name from " + table;
			if (null != searchValue && !"".equals(searchValue)) {
				sql += " and name like '%" + searchValue + "'%";
			}
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				InterfaceDetail detail = new InterfaceDetail();
				detail.setId(rs.getInt("id"));
				detail.setName(rs.getString("name"));
				resultList.add(detail);
			}
			return resultList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try{
			if (rs != null) {
				rs.close();
			}
			if (conn != null) {
				conn.close();
			}
			if (pstmt != null) {
				pstmt.close();
			}}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public List<InterfaceDetail> getInterfaceDetail(int cid) {
		String table = "openapi_interface";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			List<InterfaceDetail> resultList = new ArrayList<InterfaceDetail>();
			conn = dataSource.getConnection();
			String sql = "select id,name,detail,free,hasLogin,param,rep,repDetail,url,other from " + table + " where id = "+cid;
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				InterfaceDetail detail = new InterfaceDetail();
				detail.setId(rs.getInt("id"));
				detail.setName(rs.getString("name"));
				detail.setDetail(rs.getString("detail"));
				detail.setFree(rs.getString("free"));
				detail.setHasLogin(rs.getString("hasLogin"));
				detail.setParam(rs.getString("param"));
				detail.setResp(rs.getString("rep"));
				detail.setRepDetail(rs.getString("repDetail"));
				detail.setUrl(rs.getString("url"));
				detail.setOther(rs.getString("other"));
				resultList.add(detail);
			}
			return resultList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try{
			if (rs != null) {
				rs.close();
			}
			if (conn != null) {
				conn.close();
			}
			if (pstmt != null) {
				pstmt.close();
			}}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return null;
	}
}
